package com.example.administrator.pandanotes;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.sql.Date;
import java.text.SimpleDateFormat;
/**
 * Created by 于博 on 2018-07-14.
 * 笔记修改界面
 * 有修改按钮，点击修改笔记
 */
public class NoteUpdateActivity extends AppCompatActivity {
    //变量***************************************************************************
    private Button btnUpdate = null;
    private TextView tVBookName = null;
    private TextView tVPageNum = null;
    private EditText eTNoteCon = null;
    private Date date = null;
    private Note note = new Note();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_update);
        //初始化及绑定****************************************************************
        tVBookName = (TextView)findViewById(R.id.tVNoteUpBookName);
        eTNoteCon = (EditText)findViewById(R.id.eTNoteUpCon);
        tVPageNum = (TextView)findViewById(R.id.tVNoteUpPage);
        btnUpdate = (Button)findViewById(R.id.btnUpdateNote);
        note = (Note)getIntent().getSerializableExtra("note");
        //显示值
        tVBookName.setText(note.getBook_name());
        tVPageNum.setText(note.getNote_page());
        eTNoteCon.setText(note.getNote_content());
        //函数或事件*****************************************************************
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得值
                String Con = eTNoteCon.getText().toString();
                //审核，及异常处理
                if (Con.length()==0)
                {
                    Toast.makeText(NoteUpdateActivity.this,"笔记不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    ContentValues values = new ContentValues();
                    values.put("note_content",Con);
                    DataSupport.updateAll(Note.class, values, "str_date = ?", note.getStr_date());
                    Toast.makeText(NoteUpdateActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(NoteUpdateActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
